/**
 * kendo ui 工具类的封装
 */


(function () {

    var factory = kendo.factory = {};

    var defaultGridOptions = {
        sortable: true,
        groupable: false,
        selectable: false,
        reorderable: true,
        editable: false,
        resizable: true,
        pageable: {refresh: true, pageSize: 20, pageSizes: [20, 50, 100, 200, 500]},
        autoBind: false
    };
    var defaultListOptions = {
        sortable: true,
        groupable: false,
        selectable: false,
        editable: false,
        resizable: true
    };

    var defaultTransport = {
        contentType: 'application/json',
        dataType: 'json',
        type: 'POST'
    };


    function getTransport(url, data) {
        return $.extend({}, defaultTransport, {url: url, data: data});
    }

    function getDataSourceConfig(readUrl, idField, data) {
        return {
            schema: {
                data: function (response) {
                    return response.content || response.data || response
                },
                total: "totalElements",
                model: {id: idField}
            }
            , transport: {
                parameterMap: function (options) {
                    return kendo.stringify(options)
                },
                read: getTransport(readUrl, data)
            },
            pageSize: 20,
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        };
    }

    function getDataListSourceConfig(readUrl, idField, data, serverFiltering) {
        return {
            schema: {
                model: {id: idField},
                data: function (response) {
                    return response.content || response.data || response
                }
            }, transport: {
                parameterMap: function (options) {
                    return kendo.stringify(options)
                }, read: getTransport(readUrl, data)
            },
            serverFiltering: serverFiltering
        };
    }

    factory.getGridOptions = function (readUrl, idField, columns, readData, options) {
        return $.extend({}, defaultGridOptions, options, {
            dataSource: getDataSourceConfig(readUrl, idField, readData),
            columns: columns
        });
    };
    factory.getListOptions = function (readUrl, idField, columns, readData, options) {
        return $.extend({}, defaultListOptions, options, {
            dataSource: getDataListSourceConfig(readUrl, idField, readData, true),
            columns: columns
        });
    };
    factory.getComboxOptions = function (readUrl, idField, options) {
        return $.extend({}, options, {
            dataSource: getDataListSourceConfig(readUrl, idField, null, false)
        });
    };
    factory.orgTreeViewOptions = {
        treeView: {
            dataSource: getHierarchicalDataSource("organizationId",
                contextPath + '/sysOrganization/findAllNotSite', null,
                "children"),
            dataTextField: "name"
        }
        , valueField: "organizationId", textField: "name"
    };
    factory.clearOtherProps = function (item) {
        item._events = {};
        item._handlers = {};
        item.parent = {};
        item.__proto__ = {};
    };
    factory.formatData = function (data, format, prefix, suffix) {
        var tem = "0";
        if (null == data || "" === data)
            return tem;

        kendo.culture("zh-CN");
        tem = kendo.toString(parseFloat(data), format);

        if (prefix)
            tem = prefix + tem;

        if (suffix)
            tem += suffix;
        return tem;
    };

    factory.getFilter = function (obj) {
        var result = {filters: []};
        $.each(obj, function (k, v) {
            result.filters.push({
                field: k,
                value: v
            });
        });

        return result;
    };

    $(function () {

        function onShow(e) {
            if (e.sender.getNotifications().length == 1) {
                var element = e.element.parent(),
                    eWidth = element.width(),
                    eHeight = element.height(),
                    wWidth = $(window).width(),
                    wHeight = $(window).height(),
                    newTop, newLeft;

                newLeft = Math.floor(wWidth / 2 - eWidth / 2);
                newTop = Math.floor(wHeight / 2 - eHeight / 2);

                e.element.parent().css({top: newTop, left: newLeft});
            }
        }

        var notification = $("#notification").kendoNotification({
            position: {
                pinned: true,
                top: 30,
                right: 30
            },
            show: onShow,
            button: true,
            autoHideAfter: 2000,
            stacking: "down",
            templates: [{
                type: "info",
                template: $("#emailTemplate").html()
            }, {
                type: "error",
                template: $("#errorTemplate").html()
            }, {
                type: "success",
                template: $("#successTemplate").html()
            }]
        }).data("kendoNotification");

        // title, message, type: [info, error, success]
        kendo.factory.notify = function (title, message, type) {
            notification.show({
                title: title,
                message: message || ""
            }, type || "info");
        };

        kendo.factory.notifyInfo = function (title, message) {
            notification.show({
                title: title,
                message: message || ""
            }, "info");
        };

        kendo.factory.notifySuccess = function (message) {
            notification.show({
                message: message
            }, "success");
        };

        kendo.factory.notifyError = function (title, message) {
            notification.show({
                title: title,
                message: message || ""
            }, "error");
        };
        kendo.factory.getEnumName = function (name, enumList) {
            var arr = $.grep(enumList, function (n, i) {
                return name === n.value;
            });
            if (arr.length == 1) {
                return arr[0].text;
            }
            return name;
        }

        $(document).one("kendo:pageUnload", function () {
            if (notification) {
                notification.hide();
            }
        });
    });


    kendo.ui.Validator.prototype.options.messages = {
        required: "必填",
        pattern: "不合法",
        min: "不合法",
        max: "不合法",
        step: "不合法",
        email: "不合法邮件",
        url: "不合法URL",
        date: "不合法日期"
    };

})();

